package me.ratel.demo.wechat.entities;

import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.hash.Hashing;
import me.ratel.demo.wechat.utils.KeyValuePair;
import me.ratel.demo.wechat.utils.SignUtil;

import java.nio.charset.Charset;
import java.util.Random;

/**
 * @author Administrator
 * @time 2016/9/19 8:38
 */
public class BrandWCPayRequest {
    @JSONField(name = "appId")
    private String appId;

    @JSONField(name = "timeStamp")
    private String timestamp = String.valueOf(System.currentTimeMillis() / 1000);

    @JSONField(name = "nonceStr")
    private String nonceStr = Hashing.md5()
            .hashString(String.valueOf(new Random().nextInt(10000)), Charset.forName("UTF-8")).toString();

    @JSONField(name = "package")
    private String pack;

    @JSONField(name = "signType")
    private String signType = "MD5";

    @JSONField(name = "paySign")
    private String paySign;

    public BrandWCPayRequest(String appId, String prepayId, String apiKey) {
        setAppId(appId);
        setPack("prepay_id=" + prepayId);
        setPaySign(createPaySign(apiKey));
    }

    private String createPaySign(String apiKey) {
        return SignUtil.sign(
                new KeyValuePair<>("key", apiKey),
                new KeyValuePair[]{
                        new KeyValuePair<>("appId", getAppId()),
                        new KeyValuePair<>("timeStamp", getTimestamp()),
                        new KeyValuePair<>("nonceStr", getNonceStr()),
                        new KeyValuePair<>("package", getPack()),
                        new KeyValuePair<>("signType", getSignType()),
                }
        );
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getPack() {
        return pack;
    }

    public void setPack(String pack) {
        this.pack = pack;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }
}
